home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1997 January / macpower199701.bin / MacPowerÉIÉäÉWÉiÉã / ÉLÉáÅ[ÉrÇÃÉvÉçÉOÉâÉ}Å[ / main.c < prev   
Encoding:
C/C++ Source or Header  |  1996-11-11  |  13.4 KB  |  537 lines

  1. /*------------- include header files -------------*/
  2. #include "WindowExtensions.h"
  3.  
  4. /*------------- constant for palette window -------------*/
  5. #define K_ICON_WIDTH    32
  6. #define K_ICON_HEIGHT    32
  7.  
  8. #define K_STYLE_CEL_H        1
  9. #define K_STYLE_CEL_V        3
  10. #define K_STYLE_ICON_ID        128
  11. #define K_STYLE_MENU_ITEM    1
  12.  
  13. #define K_COLOR_CEL_H        3
  14. #define K_COLOR_CEL_V        2
  15. #define K_COLOR_ICON_ID        131
  16. #define K_COLOR_MENU_ITEM    5
  17.  
  18. #define SETUP_PAL_RECT(r, x, y, w, h) ¥
  19.             SetRect(r, x*w, y*h, (x+1)*w, (y+1)*h)
  20.  
  21. /*------------- global for program -------------*/
  22. Boolean                exit_program = false;
  23. MenuHandle            apple_menu, file_menu, edit_menu, tool_menu, win_menu;
  24. AEEventHandlerUPP    upp_launch, upp_opendoc, upp_print, upp_quit;
  25. Rect                r_new_window;
  26. short                cur_style = 0, cur_color = 0;
  27.  
  28. /*------------- global for palette window -------------*/
  29. CIconHandle        cicn_style[K_STYLE_CEL_H * K_STYLE_CEL_V];
  30. CIconHandle        cicn_color[K_COLOR_CEL_H * K_COLOR_CEL_V];
  31. WindowPtr        style_palette, color_palette;
  32. ActivateHandlerUPP    g_active_event_handler_for_doc_window;
  33.  
  34. /*------------- main -------------*/
  35. void    main(void);
  36. /*------------- initialize -------------*/
  37. OSErr    install_apple_events_handler(void);
  38. void    do_setup_appl(void);
  39. void    do_init_rom(void);
  40. /*------------- process apple events -------------*/
  41. pascal OSErr ae_launch(AppleEvent*ae, AppleEvent*rae, long refcon);
  42. pascal OSErr ae_opendoc(AppleEvent*ae, AppleEvent*rae, long refcon);
  43. pascal OSErr ae_print(AppleEvent*ae, AppleEvent*rae,  long refcon);
  44. pascal OSErr ae_quit(AppleEvent*ae, AppleEvent*rae, long refcon);
  45. /*------------- event loop -------------*/
  46. void    do_main_event_loop(void);
  47. /*------------- management menus -------------*/
  48. void    setup_checkmark(void);
  49. void    do_menu(long menu_and_item);
  50. /*------------- control mouse -------------*/
  51. void    do_mouse_down(Point pt);
  52. /*------------- management document window -------------*/
  53. void    do_update(WindowPtr win);
  54. void    create_new_window(void);
  55. void    close_window(WindowPtr wp);
  56. /*------------- management palette window -------------*/
  57. pascal void active_event_handler_for_doc_window(WindowRef win, Boolean activate);
  58. void    create_palette_windows(void);
  59. void    choose_style(int new_style);
  60. void    choose_color(int new_color);
  61. void    click_style_window(Point pt);
  62. void    click_color_window(Point pt);
  63.  
  64.  
  65. /*------------- main -------------*/
  66. void main(void)
  67. {
  68.     do_init_rom();
  69.     install_apple_events_handler();
  70.     do_setup_appl();
  71.     do_main_event_loop();
  72. }
  73.  
  74. /*------------- initialize -------------*/
  75. OSErr install_apple_events_handler(void)
  76. {
  77. OSErr    err;
  78. long    resp;
  79.     err = Gestalt(gestaltAppleEventsAttr, &resp);
  80.     if((err == noErr) && (resp & (31 - gestaltAppleEventsPresent))) {
  81.         upp_launch = NewAEEventHandlerProc(ae_launch);
  82.         upp_opendoc = NewAEEventHandlerProc(ae_opendoc);
  83.         upp_print = NewAEEventHandlerProc(ae_print);
  84.         upp_quit = NewAEEventHandlerProc(ae_quit);
  85.         err = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, upp_launch, 0, false);
  86.         err = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, upp_opendoc, 0, false);
  87.         err = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, upp_print, 0, false);
  88.         err = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, upp_quit, 0, false);
  89.     }
  90.     return err;
  91. }
  92.  
  93. void do_setup_appl(void)
  94. {
  95. Handle    h;
  96.     InsertMenu(apple_menu   = GetMenu(128), 0);
  97.     InsertMenu(file_menu    = GetMenu(129), 0);
  98.     InsertMenu(edit_menu    = GetMenu(130), 0);
  99.     InsertMenu(tool_menu    = GetMenu(131), 0);
  100.     InsertMenu(win_menu     = GetMenu(132), 0);
  101.     AddResMenu(apple_menu, 'DRVR');
  102.     DrawMenuBar();
  103.     g_active_event_handler_for_doc_window =
  104.         NewActivateHandlerProc((ProcPtr)active_event_handler_for_doc_window);
  105.     create_palette_windows();
  106.     SetRect(&r_new_window, 34, 102, 234, 302);
  107.     create_new_window();
  108. }
  109.  
  110. void do_init_rom(void)
  111. {
  112.     MaxApplZone();
  113.     MoreMasters();
  114.     MoreMasters();
  115.     MoreMasters();
  116.     MoreMasters();
  117.     InitGraf(&qd.thePort);
  118.     InitFonts();
  119.     InitWindows();
  120.     InitMenus();
  121.     TEInit();
  122.     InitDialogs(nil);
  123.     FlushEvents(everyEvent, 0);
  124.     InitCursor();
  125. }
  126.  
  127. /*------------- process apple events -------------*/
  128. pascal OSErr ae_launch(AppleEvent*ae, AppleEvent*rae, long refcon)
  129. {    return noErr;    }
  130. pascal OSErr ae_opendoc(AppleEvent*ae, AppleEvent*rae, long refcon)
  131. {    return errAEEventNotHandled;    }
  132. pascal OSErr ae_print(AppleEvent*ae, AppleEvent*rae,  long refcon)
  133. {    return errAEEventNotHandled;    }
  134. pascal OSErr ae_quit(AppleEvent*ae, AppleEvent*rae, long refcon)
  135. {
  136.     exit_program = true;
  137.     return noErr;
  138. }
  139.  
  140. /*------------- event loop -------------*/
  141. void do_main_event_loop(void)
  142. {
  143. EventRecord    an_event;
  144. char        ascii_code;
  145.     setup_checkmark();
  146.     do {
  147.         if(WaitNextEvent(everyEvent, &an_event, 0, nil)) {
  148.             switch(an_event.what) {
  149.                 case keyDown:
  150.                 case autoKey:
  151.                     ascii_code = (char)(an_event.message & charCodeMask);
  152.                     if((an_event.modifiers & cmdKey) != 0)
  153.                         do_menu(MenuKey(ascii_code));
  154.                     break;
  155.                 case updateEvt:
  156.                     do_update((WindowPtr)an_event.message);
  157.                     break;
  158.                 case mouseDown:
  159.                     do_mouse_down(an_event.where);
  160.                     break;
  161.                 case kHighLevelEvent:
  162.                     AEProcessAppleEvent(&an_event);
  163.                     break;
  164.                 case activateEvt:
  165.                     /* never come here. */
  166.                     break;
  167.                 case osEvt:
  168.                     if(((an_event.message >> 24) & 0xFF) == suspendResumeMessage) {
  169.                         if(an_event.message & resumeFlag) {
  170.                             ResumeFloatingWindows();
  171.                             SetCursor(&qd.arrow);
  172.                         } else {
  173.                             SuspendFloatingWindows();
  174.                         }
  175.                     }
  176.                     break;
  177.                 default:
  178.                     break;
  179.             }
  180.             setup_checkmark();
  181.         }
  182.     } while(!exit_program);
  183. }
  184.  
  185. /*------------- management menus -------------*/
  186. void setup_checkmark(void)
  187. {
  188. int        i;
  189.     CheckItem(win_menu, 1, ((WindowPeek)style_palette)->visible);
  190.     CheckItem(win_menu, 2, ((WindowPeek)color_palette)->visible);
  191.     for(i=0; i<(K_STYLE_CEL_H * K_STYLE_CEL_V); i++)
  192.         CheckItem(tool_menu, K_STYLE_MENU_ITEM + i, cur_style == i);
  193.     for(i=0; i<(K_COLOR_CEL_H * K_COLOR_CEL_V); i++)
  194.         CheckItem(tool_menu, K_COLOR_MENU_ITEM + i, cur_color == i);
  195. }
  196.  
  197. void do_menu(long menu_and_item)
  198. {
  199. short    itemnum;
  200. Str255    da_name;
  201.     itemnum = LoWord(menu_and_item);
  202.     switch(HiWord(menu_and_item)) {
  203.         case 128:    // apple menu
  204.             switch(itemnum) {
  205.                 case 1:
  206.                     break;
  207.                 default:
  208.                     GetItem(apple_menu, itemnum, da_name);
  209.                     OpenDeskAcc(da_name);
  210.                     break;
  211.             }
  212.             break;
  213.         case 129:    // file menu
  214.             switch(itemnum) {
  215.                 case 1:    // new window
  216.                     create_new_window();
  217.                     break;
  218.                 case 2:    // close window
  219.                     close_window(FrontNonFloatingWindow());
  220.                     break;
  221.                 default:
  222.                     exit_program = true;
  223.                     break;
  224.             }
  225.             break;
  226.         case 130:    // edit menu
  227.             break;
  228.         case 131:    // tool menu
  229.             if(itemnum < K_COLOR_MENU_ITEM) {
  230.                 choose_style(itemnum - K_STYLE_MENU_ITEM);
  231.             } else {
  232.                 choose_color(itemnum - K_COLOR_MENU_ITEM);
  233.             }
  234.             break;
  235.         case 132:    // window menu
  236.             switch(itemnum) {
  237.                 case 1:    // show/hide style palette
  238.                     if(((WindowPeek)style_palette)->visible) {
  239.                         HideReferencedWindow(style_palette);
  240.                     } else {
  241.                         ShowReferencedWindow(style_palette);
  242.                     }
  243.                     break;
  244.                 case 2:    // show/hide color palette
  245.                     if(((WindowPeek)color_palette)->visible) {
  246.                         HideReferencedWindow(color_palette);
  247.                     } else {
  248.                         ShowReferencedWindow(color_palette);
  249.                     }
  250.                     break;
  251.             }
  252.             break;
  253.     }
  254.     HiliteMenu(0);
  255. }
  256.  
  257. /*------------- control mouse -------------*/
  258. void do_mouse_down(Point pt)
  259. {
  260. WindowPtr    clicked_wp;
  261. Rect        max_r;
  262. long        new_width_and_height;
  263. short        new_width, new_height;
  264. GrafPtr        saved_port;
  265.     GetPort(&saved_port);
  266.     switch(FindWindow(pt, &clicked_wp)) {
  267.         case inGoAway:
  268.             if(TrackGoAway(clicked_wp, pt)) {
  269.                 close_window(clicked_wp);
  270.             }
  271.             break;
  272.         case inDrag:
  273.             max_r = qd.screenBits.bounds;
  274.             InsetRect(&max_r, 8, 8);
  275.             DragReferencedWindow(clicked_wp, pt, &max_r);
  276.             break;
  277.         case inGrow:
  278.             max_r.left = 100;
  279.             max_r.top  = 100;
  280.             max_r.right = qd.screenBits.bounds.right - 8;
  281.             max_r.bottom = qd.screenBits.bounds.bottom - 8;
  282.             if(new_width_and_height = GrowWindow(clicked_wp, pt, &max_r)) {
  283.                 SetPort(clicked_wp);
  284.                 new_width = LoWord(new_width_and_height);
  285.                 new_height = HiWord(new_width_and_height);
  286.                 SizeWindow(clicked_wp, new_width, new_height, false);
  287.                 InvalRect(&clicked_wp->portRect);
  288.             }
  289.             break;
  290.         case inMenuBar:
  291.             do_menu(MenuSelect(pt));
  292.             break;
  293.         case inContent:
  294.             if(clicked_wp == style_palette) {
  295.                 click_style_window(pt);
  296.             } else if(clicked_wp == color_palette) {
  297.                 click_color_window(pt);
  298.             } else if(clicked_wp != FrontNonFloatingWindow()) {
  299.                 SelectReferencedWindow(clicked_wp);
  300.             }
  301.             break;
  302.         default:
  303.             break;
  304.     }
  305.     SetPort(saved_port);
  306. }
  307.  
  308. /*------------- management document window -------------*/
  309. void do_update(WindowPtr win)
  310. {
  311. GrafPtr        saved_port;
  312. Rect        r;
  313. RGBColor    rgb;
  314. int            x, y, i;
  315. long        refcon;
  316.     GetPort(&saved_port);
  317.     SetPort(win);
  318.     BeginUpdate(win);
  319.     if(win == style_palette) {
  320.         PenSize(4, 4);
  321.         for(y=0, i=0; y<K_STYLE_CEL_V; y++)
  322.             for(x=0; x<K_STYLE_CEL_H; x++, i++){
  323.                 SETUP_PAL_RECT(&r, x, y, K_ICON_WIDTH, K_ICON_HEIGHT);
  324.                 PlotCIcon(&r, cicn_style[i]);
  325.                 if(i == cur_style) FrameRect(&r);
  326.             }
  327.         PenSize(1, 1);
  328.     } else if(win == color_palette) {
  329.         PenSize(4, 4);
  330.         for(y=0, i=0; y<K_COLOR_CEL_V; y++)
  331.             for(x=0; x<K_COLOR_CEL_H; x++, i++) {
  332.                 SETUP_PAL_RECT(&r, x, y, K_ICON_WIDTH, K_ICON_HEIGHT);
  333.                 PlotCIcon(&r, cicn_color[i]);
  334.                 if(i == cur_color) FrameRect(&r);
  335.             }
  336.         PenSize(1, 1);
  337.     } else {
  338.         EraseRect(&win->portRect);
  339.         r = win->portRect;
  340.         r.right -= 14;
  341.         r.bottom -= 14;
  342.         InsetRect(&r, 16, 16);
  343.         refcon = GetExtWRefCon(win);
  344.         switch(refcon % 256) {
  345.             case 0:    ForeColor(redColor);        break;
  346.             case 1:    ForeColor(greenColor);        break;
  347.             case 2:    ForeColor(blueColor);        break;
  348.             case 3:    ForeColor(yellowColor);        break;
  349.             case 4:    ForeColor(magentaColor);    break;
  350.             case 5:    ForeColor(blackColor);        break;
  351.         }
  352.         switch(refcon >> 8) {
  353.             case 0:    PaintOval(&r);    break;
  354.             case 1:    PaintRect(&r);    break;
  355.             case 2:    PaintRoundRect(&r,
  356.                         (r.right - r.left) / 6,
  357.                         (r.bottom - r.top) / 6);    break;
  358.         }
  359.         ForeColor(blackColor);
  360.         DrawGrowIcon(win);
  361.     }
  362.     EndUpdate(win);
  363.     SetPort(saved_port);
  364. }
  365.  
  366. void create_new_window(void)
  367. {
  368. WindowPtr    win;
  369.     OffsetRect(&r_new_window, 10, 16);
  370.     if(r_new_window.bottom > qd.screenBits.bounds.bottom)
  371.         OffsetRect(&r_new_window, 0, - r_new_window.top + 50);
  372.     if(r_new_window.right > qd.screenBits.bounds.right)
  373.         OffsetRect(&r_new_window, - r_new_window.left + 10, 0);
  374.     if(NewWindowReference(
  375.             &win,
  376.             &r_new_window,
  377.             "¥pUntitled",
  378.             true,
  379.             (kHasCloseBoxMask | kHasDocumentTitlebarMask | kHasGrowBoxMask),
  380.             (void*)-1,
  381.             0,
  382.             active_event_handler_for_doc_window)) {
  383.         ExitToShell();
  384.     }
  385.     SetExtWRefCon(win, 0);
  386. }
  387.  
  388. void close_window(WindowPtr win)
  389. {
  390.     if(win == style_palette) {
  391.         HideReferencedWindow(style_palette);
  392.     } else if(win == color_palette) {
  393.         HideReferencedWindow(color_palette);
  394.     } else if(win) {
  395.         DisposeWindowReference(win);
  396.     }
  397. }
  398.  
  399. /*------------- management palette window -------------*/
  400. pascal void active_event_handler_for_doc_window(WindowRef win, Boolean activate)
  401. {
  402. GrafPtr    saved_port;
  403. long    refcon;
  404.     GetPort(&saved_port);
  405.     SetPort((GrafPtr) win);
  406.     DrawGrowIcon(win);
  407.     if(activate) {
  408.         refcon = GetExtWRefCon(win);
  409.         choose_style(refcon / 256);
  410.         choose_color(refcon % 256);
  411.     } else {
  412.         SetPort(saved_port);
  413.     }
  414. }
  415.  
  416. void create_palette_windows(void)
  417. {
  418. Rect    r;
  419. int        x, y, i;
  420.     SetRect(&r,    4,
  421.                 40,
  422.                 4 + K_ICON_WIDTH * K_STYLE_CEL_H,
  423.                 40 + K_ICON_HEIGHT * K_STYLE_CEL_V);
  424.     if(NewWindowReference(
  425.             &style_palette,
  426.             &r,
  427.             "¥pStyle",
  428.             true,
  429.             (kHasCloseBoxMask | kHasPaletteTitlebarMask),
  430.             (void*)-1,
  431.             0,
  432.             nil)) {
  433.         ExitToShell();
  434.     }
  435.     SetRect(&r, r.right + 20,
  436.                 28,
  437.                 r.right + 20 + K_ICON_WIDTH * K_COLOR_CEL_H,
  438.                 28 + K_ICON_HEIGHT * K_COLOR_CEL_V);
  439.     if(NewWindowReference(
  440.             &color_palette,
  441.             &r,
  442.             "¥pColor",
  443.             true,
  444.             (kHasCloseBoxMask | kHasSideTitleBarMask | kHasPaletteTitlebarMask),
  445.             (void*)-1,
  446.             0,
  447.             nil)) {
  448.         ExitToShell();
  449.     }
  450.     for(y=0, i=0; y<K_STYLE_CEL_V; y++)
  451.         for(x=0; x<K_STYLE_CEL_H; x++, i++)
  452.             cicn_style[i] = GetCIcon(K_STYLE_ICON_ID + i);
  453.     for(y=0, i=0; y<K_COLOR_CEL_V; y++)
  454.         for(x=0; x<K_COLOR_CEL_H; x++, i++)
  455.             cicn_color[i] = GetCIcon(K_COLOR_ICON_ID + i);
  456. }
  457.  
  458. void choose_style(int new_style)
  459. {
  460. WindowPtr    win;
  461. GrafPtr        saved_port;
  462. long        refcon;
  463.     GetPort(&saved_port);
  464.     win = FrontNonFloatingWindow();
  465.     if(cur_style != new_style) {
  466.         if(win) {
  467.             refcon = (GetExtWRefCon(win) & 0x000000ff) | (new_style << 8);
  468.             SetExtWRefCon(win, refcon);
  469.             SetPort(win);
  470.             InvalRect(&win->portRect);
  471.         }
  472.         cur_style = new_style;
  473.         SetPort(style_palette);
  474.         InvalRect(&style_palette->portRect);
  475.     }
  476.     SetPort(saved_port);
  477. }
  478.  
  479. void choose_color(int new_color)
  480. {
  481. WindowPtr    win;
  482. GrafPtr        saved_port;
  483. long        refcon;
  484.     GetPort(&saved_port);
  485.     win = FrontNonFloatingWindow();
  486.     if(cur_color != new_color) {
  487.         if(win) {
  488.             refcon = (GetExtWRefCon(win) & 0x0000ff00) | (new_color);
  489.             SetExtWRefCon(win, refcon);
  490.             SetPort(win);
  491.             InvalRect(&win->portRect);
  492.         }
  493.         cur_color = new_color;
  494.         SetPort(color_palette);
  495.         InvalRect(&color_palette->portRect);
  496.     }
  497.     SetPort(saved_port);
  498. }
  499.  
  500. void click_style_window(Point pt)
  501. {
  502. Rect    r;
  503. int        x, y, i;
  504. GrafPtr    saved_port;
  505.     GetPort(&saved_port);
  506.     SetPort(style_palette);
  507.     GlobalToLocal(&pt);
  508.     for(y=0, i=0; y<K_STYLE_CEL_V; y++)
  509.         for(x=0; x<K_STYLE_CEL_H; x++, i++) {
  510.             SETUP_PAL_RECT(&r, x, y, K_ICON_WIDTH, K_ICON_HEIGHT);
  511.             if(PtInRect(pt, &r)) {
  512.                 choose_style(i);
  513.                 break;
  514.             }
  515.         }
  516.     SetPort(saved_port);
  517. }
  518.  
  519. void click_color_window(Point pt)
  520. {
  521. Rect    r;
  522. int        x, y, i;
  523. GrafPtr    saved_port;
  524.     GetPort(&saved_port);
  525.     SetPort(color_palette);
  526.     GlobalToLocal(&pt);
  527.     for(y=0, i=0; y<K_COLOR_CEL_V; y++)
  528.         for(x=0; x<K_COLOR_CEL_H; x++, i++) {
  529.             SETUP_PAL_RECT(&r, x, y, K_ICON_WIDTH, K_ICON_HEIGHT);
  530.             if(PtInRect(pt, &r)) {
  531.                 choose_color(i);
  532.                 break;
  533.             }
  534.         }
  535.     SetPort(saved_port);
  536. }
  537.